$(function(){

if(window.location.hash.split("#")[1] != "" && window.location.hash.split("#")[1] != undefined)
{
	id = "#anchor_" + window.location.hash.split("#")[1];
	
	$(id).addClass('ui-state-highlight');
}

$("#module-carriers-table").find('tr')
	.mouseover(function(){
	$(this).addClass('ui-state-hover');
	})
	.mouseout(function(){
	$(this).removeClass('ui-state-hover');
	});

change_contract_type();

$( "#module-add-new" )
			.button()
			.click(function() {
				window.document.location.href = siteUrl+'/add';
			});

			
$( "#module-export-xls" )
			.button()
			.click(function() {
				//$( "#dialog-form" ).dialog( "open" );
			});

$( "#module-import-xls" )
			.button()
			.click(function() {
				//$( "#dialog-form" ).dialog( "open" );
			});


// save settings	
$( "#module-save-settings" )
			.button()
			.click(function() {	
                                save_settings();                                
			});
			
$( "#module-save-carrier" )
			.button()
			.click(function() {	
								$( "#username_status" ).html('');
                                save_new_carrier();
			});

$( "#module-edit-carrier" )
			.button()
			.click(function() {	
								$( "#username_status" ).html('');
                                edit_carrier(carrier_ID);
			});

			
$( "#username" )
			.keypress(function(){			
				var text = $( "#username" ).val().toUpperCase();				
				$( "#username" ).val(text);			
			}).keyup(function(){			
				var text = $( "#username" ).val().toUpperCase();				
				$( "#username" ).val(text);		
				check_username();
			});

			

$( "#contract_type" ).change(function(){

change_contract_type();

});	

// deactivate selected elements	
$('.deactivated').click(function(){
				$(this).blur();
			}).focus(function(){
				$(this).blur();
			}).select(function(){
				$(this).blur();
			}).change(function(){
				$(this).blur();
			});		

// Dialog			
$('#enable').dialog({
				autoOpen: false,
				width: 600,
				buttons: {
						"Yes": function() { 
							enable_carrier(carrier_ID,$(this));
						}, 
						"No": function() { 
							$(this).dialog("close"); 
						} 
				}
});
$('#disable').dialog({
				autoOpen: false,
				width: 600,
				buttons: {
						"Yes": function() { 
							disable_carrier(carrier_ID,$(this));
						}, 
						"No": function() { 
							$(this).dialog("close"); 
						} 
				}
});
	

$( ".edit").each(function(){

	$(this).button().addClass('options-button');
	
	var status = $(this).attr("rel");
	var carrier_id = $(this).attr("name");

	if(status == 0)
	{
		$(this).removeClass('ui-state-disabled').addClass('ui-state-highlight');
	}

	else if(status == 1)
	{
		$(this).addClass('ui-state-default');
	}

	$(this).click(function(){
			window.document.location.href = 'edit/'+carrier_id;
	});
	
});

$("#enable, #disable").hide();	
$(".status_0").addClass('ui-state-disabled');
	
$( ".options").each(function(){

	$(this).button().addClass('options-button');
	
	var status = $(this).attr("rel");
	var carrier_name = $(this).attr("title");
	var carrier_id = $(this).attr("id");

	if(status == 0)
	{
		$(this).removeClass('ui-state-disabled').addClass('ui-state-highlight');
	}

	else if(status == 1)
	{
		$(this).addClass('ui-state-default');
	}

	$(this).click(function(){
	
	var status = $(this).attr("rel");
	
			if(status == 0)
			{
				carrier_ID = carrier_id;
				$('.carrier_name').html(carrier_name);
				$('#enable').dialog( "open" );							
			}
		
			else if(status == 1)
			{
				carrier_ID = carrier_id;
				$('.carrier_name').html(carrier_name);			
				$('#disable').dialog( "open" );							
			}			
	});
	
});
	
	/* SEARCH CARRIERS */
	
	// submit button action
	$("#filter-submit").button();
	
	// replace value of the field
	$("#username_field").click(function(){	
	
		if($(this).val() == term_username)
		{
			$(this).val("");
		}
	});	
});